//
//  AppDelegate.swift
//  swiftim
//
//  Created by yangjian on 2023/5/30.
//

import UIKit
import UserNotifications
import HyphenateChat

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        config()
        return true
    }
    
    
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        DispatchQueue.main.async {
            EMClient.shared().registerForRemoteNotifications(withDeviceToken: deviceToken) { error in
                print(error as Any)
            }
        }
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        EMClient.shared().applicationDidEnterBackground(application)
    }
    func applicationWillEnterForeground(_ application: UIApplication) {
        EMClient.shared().applicationWillEnterForeground(application)
    }
}

extension AppDelegate {

    func config(){
        
        app_safeAreaInsets = window?.safeAreaInsets ?? UIEdgeInsets.zero
        //im模块,初始化
        EMAppConfig.share.configIMClient(appdelegate: self)
        
        //推送模块
        EMAppConfig.share.registerUserNotification(appdelegate: self)
        
        //图片选择器
        EMAppConfig.share.cunfigImageBrowser()
        
        
        
        
        
//        //发群聊消息的示例
//        let msgBody = EMTextMessageBody(text: "abc")
//        var msg = EMChatMessage(conversationID: "某群组的id", body: msgBody, ext: nil)
//        
//        //重点是这句需要有的.默认值是chat,也就是单聊
//        msg.chatType = .groupChat
//        
//        EMClient.shared().chatManager?.send(msg, progress: nil,completion: { msg, error in
//            if let error {
//                //发消息错误,show error ...
//                return
//            }
//        })
        
        
        
    }
    
}
